#!/usr/bin/python
# -*- coding: UTF-8 -*-
import os, sys, time, threading
sys.path.append('../')
from pb import *
#############################################################
server_ip = "192.168.1.254"
server_port = 17954

#server_ip = "192.168.1.250"
#server_port = 8800

try:
    (first_token, max_players) = (int(sys.argv[1]), int(sys.argv[2]))
except:
    (first_token, max_players) = (1000, 10)

print "target server is : %s, %s  [pid: %d]\n" %(server_ip ,server_port,  os.getpid())
print "first_token: %d, max players count %d\n" %(first_token, max_players)
 
class worker (threading.Thread): 
    def __init__(self, token, passwd):  
        threading.Thread.__init__(self)        
        self.thread_stop = False
        
        self.sockobj = socket.socket(socket.AF_INET,socket.SOCK_STREAM)        
        self.sockobj.connect((server_ip, server_port))
        
        self.player = player(self.sockobj, token, passwd)
        
    def stop(self):          
        self.thread_stop = True  
        self.sockobj.close()
        self._Thread__stop ()

    def run(self):       
        if not self.player.have_login () :  self.player.login()
        
        buf = ""
        has_init_pet_style = False        
        pet_style = 0
        
        while not self.thread_stop:  
            recv_buf = self.sockobj.recv(65535)
            buf = buf + recv_buf
                        
            code_len =  get_code_len(buf)
            #print "============================ %d, %d" %(code_len, (len(buf)))
            while code_len > 0 and len(buf) >= code_len:            
                (total_len, buf) =  decode_int(buf)
                (head_len, head, buf) =  head_parser(buf)
                if total_len > head_len + 9:                     
                    (body_len, buf) =  decode_int(buf)
                else:
                    body_len = 0

                #print "%d, %d, %d, %d, %d"  %(self.gid, total_len, head_len, body_len, head.msgid)                        
                if head.msgret == 0 :                
                    if head.msgid == 1003:
                        self.player.parser (head.msgid, buf[0:body_len])
                        self.player.get_user_info ()
                        
                    elif head.msgid == 1005:
                        if has_init_pet_style == False:
                            self.player.parser (head.msgid, buf[0:body_len])                        
                            self.player.gm_open_sys ()
                            self.player.get_pet_info ()
                            has_init_pet_style = True

                    elif head.msgid == 1185:                        
                        pet_style = self.player.parser (head.msgid, buf[0:body_len])

                        if pet_style > 0:
                            if pet_style == 1:  pet_style = 2
                            else:  pet_style = 1
                            self.player.change_pet_style (pet_style)
                            time.sleep(2)
                else:
                    print "server return err user: %s,  msgid: %d, ret: %d" %(self.player.token, head.msgid , head.msgret)                            
                    
                buf = buf[body_len+1:]
                code_len = get_code_len (buf)
        
def main_run(): 
    thread_list = []
    for i in range(max_players) :
        token = repr(first_token + i)
        thread = worker(token, "hc2013")
        thread_list.append (thread)
        
    for i in range(len(thread_list)) :    
        thread_list[i].start()       
    
    while threading.active_count() > 1:
        print "%d thread is alive" %threading.active_count()
        print "input 'q' to quit, other to contiue!"        
        keyboard_input =  raw_input()
        print "your input is: %s" %keyboard_input
        if keyboard_input == 'q':
            for i in range(len(thread_list)) :
                thread_list[i].stop()
            return;
            
        time.sleep(0.5)

if __name__ == '__main__':  
    main_run()
